package activity;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import com.example.fruit.GameActivity;
import com.example.fruit.R;
import com.example.fruit.R.string;
import com.example.input.topscore;
import com.example.manager.ResourcesManager;

import android.R.bool;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Typeface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.method.KeyListener;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class RegisterActivity extends Activity {
	private static String SOAP_ACTIONREGISTER = "http://tempuri.org/registerPlayer";
	private static String METHOD_REGISTER = "registerPlayer";
	private static String NAMESPACE = "http://tempuri.org/";
	private static String URL = "http://serviceninja.somee.com/servicehighscore.asmx?wsdl";
	
	Button btnok,btncancel;
	EditText etuser,etpass,ethoten,etdiachi,etngaysinh;
	GameActivity activity;
	final Context context = this;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register);
		
		activity=ResourcesManager.getInstance().activity;
		btnok= (Button)findViewById(R.id.btnOK);
		btncancel= (Button)findViewById(R.id.btnCancel);
		etuser=(EditText)findViewById(R.id.user);
		etpass=(EditText)findViewById(R.id.pass);
		ethoten=(EditText)findViewById(R.id.hoten);
		etdiachi=(EditText)findViewById(R.id.diachi);
		etngaysinh=(EditText)findViewById(R.id.ngaysinh);
		
		btnok.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				btnok.setBackgroundResource(R.drawable.up);
				boolean f=false;
				String s="";
				if(etuser.getText().toString().length()==0){
					s+="username ";
					f=true;
				}
				if(etpass.getText().toString().length()==0){
					s+="pass";
					f=true;
				}
				if(f)
					Toast.makeText(getApplicationContext(), "Vui lòng nhập " + s + "!", Toast.LENGTH_SHORT).show();
				else{
					MyAsytask task=new MyAsytask();
					task.execute();
				}
			}
		});
		btncancel.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				btncancel.setBackgroundResource(R.drawable.up);
				RegisterActivity.this.finish();
			}
		});
	}
	
	
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
	}

	ProgressDialog dialog;
	class MyAsytask extends AsyncTask<Void, Void, String>
	{

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			activity.runOnUiThread(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					dialog = ProgressDialog.show(RegisterActivity.this, "", 
		                    "Loading. Please wait...", true);
					dialog.show();
				}
			});
			
		}

		@Override
		protected String doInBackground(Void... params) {
			// TODO Auto-generated method stub
			SoapObject request = new SoapObject(NAMESPACE, METHOD_REGISTER);
			
			// Use this to add parameters
			request.addProperty("username", etuser.getText().toString());
			request.addProperty("pass", etpass.getText().toString());
			request.addProperty("hoten", ethoten.getText().toString());
			request.addProperty("diachi", etdiachi.getText().toString());
			request.addProperty("ngaysinh", etngaysinh.getText().toString());
			// Declare the version of the SOAP request
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);
			String s = null;
			envelope.setOutputSoapObject(request);
			envelope.dotNet = true;
			try {
				HttpTransportSE androidHttpTransport = new HttpTransportSE(
						URL);

				// this is the actual part that will call the webservice
				androidHttpTransport.call(SOAP_ACTIONREGISTER, envelope);

				// Get the SoapResult from the envelope body.
				SoapObject result = (SoapObject) envelope.bodyIn; //the loadscoreresponse
				if(result!=null){
					s=result.getProperty(0).toString();
				}
				//Log.d("result",pii.getProperty(0).toString()+"");
			} catch (Exception e) {
				e.printStackTrace();
				Log.d("asdad",e.getMessage());
			}
			return s;
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			if(result.equals("true"))
			{
				activity.runOnUiThread(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						dialog.dismiss();
						AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
						 
						// set title
						alertDialogBuilder.setTitle("Đăng ký thành công!");
			 
						// set dialog message
						alertDialogBuilder
							.setCancelable(false)
							.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,int id) {
									// if this button is clicked, close
									// current activity
									RegisterActivity.this.finish();
								}
							  })
							.create()
							.show();
					}
				});
			}else
			{
				activity.runOnUiThread(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						Toast.makeText(getApplicationContext(),"Username này đã có.\nVui lòng nhập username khác!", Toast.LENGTH_LONG).show();
					}
				});
			}
			//Toast.makeText(getApplicationContext(), result+"", Toast.LENGTH_LONG).show();
		}

	}
}
